package dz.chuxin.app.core;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;

import dz.chuxin.app.R;
import dz.chuxin.framework.widget.recycler.BaseRecyclerAdapter;
import dz.chuxin.framework.widget.recycler.BaseViewHolder;
import dz.chuxin.framework.widget.recycler.ChuXinRecyclerView;
import dz.chuxin.framework.widget.recycler.ILayoutManager;
import dz.chuxin.framework.widget.recycler.MyLinearLayoutManager;

/**
 * 支持下拉列表的Activity基类
 * Created by chuxin on 2016/7/1.
 */
public abstract class BaseRecyclerActivity<T> extends BaseActivity
        implements ChuXinRecyclerView.OnRecyclerRefreshListener {

    protected ChuXinRecyclerView mRecyclerView;
    protected BaseRecyclerAdapter mRecyclerAdapter;
    protected ArrayList<T> mDataList = new ArrayList<>();

    protected int mCurrentPage = 1;

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_base_recycler, -1);
        mRecyclerView = (ChuXinRecyclerView) findViewById(R.id.chuxin_recycler_view);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        mRecyclerView.setOnRecyclerRefreshListener(this);
        mRecyclerView.setLayoutManager(getLayoutManager());
        mRecyclerView.addItemDecoration(getItemDecoration());
        mRecyclerView.enableLoadMore(true);

        mRecyclerAdapter = new RecyclerAdapter();
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    // 获取列表项修饰，主要用于列表分隔线
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    // 获取LayoutManager，默认是LinearLayoutManager
    protected ILayoutManager getLayoutManager() {
        return new MyLinearLayoutManager(this);
    }

    // ----------------------------------------------------------------------
    // BaseRecyclerAdapter适配器简单实现，真正的实现抛给子类去做
    class RecyclerAdapter extends BaseRecyclerAdapter {
        @Override
        protected BaseViewHolder onCreateNormalViewHolder(ViewGroup parent, int viewType) {
            return getRecyclerViewHolder(parent, viewType);
        }

        @Override
        protected int getDataCount() {
            return mDataList == null ? 0 : mDataList.size();
        }

        @Override
        public int getDataViewType(int position) {
            return getViewType(position);
        }

        @Override
        public boolean isSectionHeader(int position) {
            return isItemSectionHeader(position);
        }
    }

    // 指定位置的列表项是否是分组的头，在BaseSectionRecyclerActivity中使用
    protected boolean isItemSectionHeader(int position) {
        return false;
    }

    protected int getViewType(int position) {
        return BaseRecyclerAdapter.VIEW_TYPE_NORMAL;
    }

    protected abstract BaseViewHolder getRecyclerViewHolder(ViewGroup parent, int viewType);
}
